package com.enjoy.aop.dynamicProxy.cglib;


import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.NoOp;

public class Client {
    public static void main(String[] args) throws InterruptedException {

        for(;;){

            // 代理类class文件存入磁盘方便我们反编译
//        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "F:\\enjoy-spring\\spring-annotation\\code");
            // 通过cglib获取代理实例的过程
            Enhancer enhancer = new Enhancer();
            // 设置enhancer的父类
            enhancer.setSuperclass(UserService.class);
            // 设置enhancer的回调对象
//        enhancer.setCallback(new HelloInterceptor());
            enhancer.setCallbackFilter(new CglibCallbackFilter());


            MethodInterceptor helloInterceptor = new HelloInterceptor();
            MethodInterceptor eatInterceptor = new EatInterceptor();
            Callback[] callbacks = {helloInterceptor,eatInterceptor, NoOp.INSTANCE};
            enhancer.setCallbacks(callbacks);
            // 创建代理对象
            UserService proxy = (UserService) enhancer.create();
            // 通过代理对象调用目标方法
            proxy.sayEat();

            proxy.sayHello();

            Thread.sleep(100);
        }

    }
}
